{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3371497b",
   "metadata": {
    "jupyter": {
     "source_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "# /// script\n",
    "# requires-python = \">=3.12\"\n",
    "# dependencies = [\n",
    "#     \"ipywidgets\",\n",
    "#     \"lonboard\",\n",
    "# ]\n",
    "# ///"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e328a21f-5254-42ee-9683-136561d96886",
   "metadata": {},
   "source": [
    "# Clicked point\n",
    "\n",
    "This notebook demonstrates how you can access the coordinate of the most recent click on a Lonboard map and update a set of widgets to display the x and y coordinates using the [`ipywidgets.observe`](https://ipywidgets.readthedocs.io/en/8.1.5/examples/Widget%20Events.html#traitlet-events) method.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ef63b4d-0fd6-4984-bf8e-2a8d2d86f836",
   "metadata": {},
   "source": [
    "## Dependencies\n",
    "\n",
    "Install [`uv`](https://docs.astral.sh/uv) and then launch this notebook with:\n",
    "\n",
    "```\n",
    "uvx juv run examples/clicked-point.ipynb\n",
    "```\n",
    "\n",
    "(The `uvx` command is included when installing `uv`)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "87de8097-4bb9-4180-a3ca-c82c826c38d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import Tuple\n",
    "\n",
    "import ipywidgets as widgets\n",
    "\n",
    "from lonboard import Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f986a64e-269f-4966-93b0-cfa5bf1ba882",
   "metadata": {},
   "outputs": [],
   "source": [
    "## Create a Lonboard map and two widgets to display the coordinate\n",
    "m = Map(layers=[], view_state={\"zoom\": 1.5}, height=600)\n",
    "clicked_x_display = widgets.FloatText(description=\"last clicked x:\")\n",
    "clicked_y_display = widgets.FloatText(description=\"last clicked y:\")\n",
    "\n",
    "\n",
    "def on_map_click(coordinate: Tuple[float, float]) -> None:\n",
    "    x, y = coordinate\n",
    "    clicked_x_display.value = x\n",
    "    clicked_y_display.value = y\n",
    "\n",
    "\n",
    "m.on_click(on_map_click)\n",
    "\n",
    "## show the widgets\n",
    "widgets.VBox(\n",
    "    [\n",
    "        m,\n",
    "        clicked_x_display,\n",
    "        clicked_y_display,\n",
    "    ],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05a0e681-899e-4a89-bf86-db55cdbef608",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "lonboard",
   "language": "python",
   "name": "lonboard"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
